import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

let router=new Router({
    mode:"hash",//1、hash哈希：有#号。2、history历史：没有#号
    base:process.env.BASE_URL, //自动获取根目录路径
    scrollBehavior:(to,from,position)=>{
        if(position){
            return position
        }else{
            return {x:0,y:0}
        }
    },
    routes:[
        {
            path: '/',
            name: 'index',
            component: () => import('./pages/index')
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('./pages/manage/login/index')
        },
        {
            path: '/home',
            name: 'home',
            meta:{auth:true},
            component: () => import('./pages/manage/home/index')
        },
        {
            path: '/videoadd',
            name: 'videoadd',
            meta:{auth:true},
            component: () => import('./pages/manage/video/add')
        },
        {
            path: '/applicant',
            name: 'applicant',
            meta:{auth:true},
            component: () => import('./pages/manage/applicant/index')
        },

    ]
});
router.beforeEach((to,from,next)=>{
        if (localStorage['isLogin'] ==="true" || localStorage['isLogin'] == true){ //如果已经登陆过
            if (to.path == '/login') { //如果登录成功访问登录页跳转到主页
                next({path: '/home'})
            }else{
                next();
            }
        } else { //如果没有登陆过
            if (to.meta.auth){
                next("/login");
            }else{
                next();
            }
        }
});
export default router;